package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Attractions;
import cn.wolfcode.luowowo.article.domain.Fans;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.AttractionsQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IUesrInfoCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("mine")
public class MineContrller {
    @Reference  //不同服务间引入
    private IUserInfoService userInfoService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelStatisRedisService travelStatisRedisService;
    @Reference
    private IUesrInfoCacheService uesrInfoCacheService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IAttractionsService attractionsService;

    @Reference
    private IFansService fansService;






    @RequestMapping("/home")
    public String home(Model model, @UserParam UserInfo userInfo, @ModelAttribute("qo")AttractionsQuery qo){
        //获得当前用户
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());

        model.addAttribute("user",user);

        //查询当前登录用户的游记列表
        TravelQuery qo2=new TravelQuery();
        /*List<Travel> travels =  travelService.getTravel(userInfo.getId());
        for (Travel travel : travels) {
            Long id = travel.getId();
            TravelStatisVO thumnum = travelStatisRedisService.getVoByTravelId(id);
            model.addAttribute("thumnum",thumnum);


        }*/


        //查询当前用户的游记(已发布的按发布时间倒序排列)
        qo2.setOrderType(1);
        //状态为发布
        qo2.setState(Travel.STATE_RELEASE);
        qo2.setAuthorId(userInfo.getId());
        PageInfo pageInfo=travelService.query(qo2);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo",pageInfo);

       //查询当前用户拥有的游记数
        int count=travelService.getCount(userInfo.getId());
        model.addAttribute("count",count);



        //关注
       List<Fans> fans =  fansService.selectFansByUserId(userInfo.getId());

           model.addAttribute("size",fans==null?0:fans.size());
        System.out.println(fans);

        model.addAttribute("fans",fans);

        //点评
        List<Attractions> reviewSize= attractionsService.selectReviewSize(userInfo.getId());//用户点评的总数
        int sizes = 0;
        for (Attractions attractions : reviewSize) {
            if (attractions.getReviewNum() > 0 && attractions.getReviewNum() != null){
                sizes ++;
            }
        }


        model.addAttribute("sizes", sizes);
        model.addAttribute("reviews", attractionsService.selectForList(qo ,userInfo.getId()));


        return "/mine/homepage"; //我的窝
    }


    //我的游记
    @RequestMapping("/travel")
    public String travel(Long id,Model model,@UserParam UserInfo userInfo,TravelQuery qo) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Travel travel = travelService.selectTravelById(id);
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());

        model.addAttribute("user",user);
        //查询当前用户的所有游记
        qo.setAuthorId(userInfo.getId());
        PageInfo pageInfo=travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        //查询当前用户拥有的游记数
        int count=travelService.getCount(userInfo.getId());
        model.addAttribute("count",count);



        //回复数
        int thumbsupnum = 0;
        List<Travel> travels = travelService.selectAll();
        for (Travel t : travels) {
            thumbsupnum += t.getThumbsupnum();

        }
        model.addAttribute("thumbsupnum",thumbsupnum);
        //阅读数
        int replynum = 0;
        for (Travel t : travels) {
            replynum += t.getReplynum();

        }
        model.addAttribute("replynum",replynum);


        return "/mine/mytravelnotes";
    }
    //设置
    @RequestMapping("/setting")
    @RequireLogin
    public String setting(Model model, @UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", user);
        return "mine/setting";
    }
    // 保存用户个人信息
    @RequestMapping("/saveUserInfo")
    @RequireLogin
    public String saveUserInfo(UserInfo userInfo){
        userInfoService.updateByPrimaryKey(userInfo);
        return "redirect:/mine/setting";
    }
    //我的头像
    //我的头像
    @RequestMapping("/avater")
    @RequireLogin
    public String avater(Model model, @UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", user);
        return "mine/avater";
    }
    //文件上传的路径:在e:/img/upload
    @Value("${file.path}")
    private String filePath;

    //上传图片
    @RequestMapping("/uploadImg")
    @ResponseBody
    @RequireLogin
    public Object coverImageUpload(MultipartFile pic, @UserParam UserInfo userInfo) {


        String upload = UploadUtil.upload(pic, filePath);
        // 更新用户头像信息
        userInfoService.saveOrUpdateheadImgUrl("/"+upload,userInfo);
        return new AjaxResult(true,"").addData(upload);
    }

    //查询昵称,用作黑名单
    @RequestMapping("/selectBadUser")
    @RequireLogin
    public Object selectBadUser(Model model,@UserParam UserInfo userInfo,String nickname) {
        UserInfo u = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", u);

        //检查黑名单功能然后修改的
        if (nickname != null){

            UserInfo user =  userInfoService.selectBadUser(nickname);
            model.addAttribute("baduser",user);
        }
        List<String> list = uesrInfoCacheService.selectBadUserList(userInfo.getId());
        model.addAttribute("list",list);
        return "/mine/blacklist";
    }






    //绑定设置
    @RequireLogin
    @RequestMapping("/sns")
    public String sns(Model model, @UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", user);
        return "/mine/sns";
    }


    //账号安全
    @RequestMapping("/security")
    @RequireLogin
    public String security(Model model, @UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", user);
        return "/mine/security";
    }
    //修改密码
    @RequestMapping("/updatePasswordOrPhoneById")
    @RequireLogin
    public String updatePasswordOrPhoneById(@UserParam UserInfo userInfo,String password,String phone) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        if ((password == null && password.equals("")) && phone == null && phone.length() == 0){
            userInfoService.updatePasswordOrPhoneById(user);

        }
        if (password != null && !password.equals("")){


            userInfo.setPassword(password);
            userInfo.setPhone(user.getPhone());
            userInfoService.updatePasswordOrPhoneById(userInfo);

        }



        if (phone != null && phone.length() != 0){

            userInfo.setPhone(phone);
            userInfo.setPassword(user.getPassword());
            userInfoService.updatePasswordOrPhoneById(userInfo);
        }




       if ((password != null && !password.equals("")) && phone != null && phone.length() != 0){
            user.setPhone(phone);
            user.setPassword(password);
            userInfoService.updatePasswordOrPhoneById(user);

        }
        /*
        if ((password != null && !password.equals("")) && phone == null && phone.length() == 0){
            user.setPassword(password);
            userInfoService.updatePasswordOrPhoneById(user);

        }*/
        /*
        if ((password != null && !password.equals("")) && phone == null && phone.length() == 0){
            user.setPassword(password);
            userInfoService.updatePasswordOrPhoneById(user);

        }*/





        return "redirect:/mine/security";
    }

    //查询我的黑名单列表
    @RequestMapping("/blacklist")
    @RequireLogin
    public String blacklist(Model model, @UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", user);
        List<String> list = uesrInfoCacheService.selectBadUserList(userInfo.getId());
        System.out.println(".........");
        System.out.println(list);
        model.addAttribute("list",list);
        return "/mine/blacklist";
    }
    // 加入黑名单
    @RequestMapping("/addBadUser")
    @RequireLogin
    public String addBadUser(Model model,@UserParam UserInfo userInfo,String nickname){
        uesrInfoCacheService.addBadUser(userInfo.getId(),nickname);
        return "redirect:/mine/blacklist";
    }
    // 移除黑名单
    @RequestMapping("/removeBadUser")
    @RequireLogin
    public String removeBadUser(Model model,@UserParam UserInfo userInfo,String nickname){
        uesrInfoCacheService.removeBadUser(userInfo.getId(),nickname);
        return "/mine/blacklist";
    }


    //我的钱包
    @RequestMapping("/wallet")
    @RequireLogin
    public String wallet(Model model, @UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", user);

        return "/mine/wallet";
    }
    //打卡
    @RequestMapping("/addCoin")
    @ResponseBody
    @RequireLogin
    public String addCoin(Model model, @UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user", user);
           
        userInfoService.addCoin(2,user.getId());
        userInfoService.updateByPrimaryKey(user);

        return "/mine/home";
    }





    /*//我的收藏
    @RequestMapping("/travelcollection")
    @RequireLogin
    public String travelcollection(Model model, @UserParam UserInfo userInfo) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user",user);

        //获取到所有的vo数据
        List<TravelStatisVO> vos =  travelStatisRedisService.selectCollectionTravelList(userInfo.getId());

        List<Travel> alltravel = new ArrayList<>();
        for (TravelStatisVO vo : vos) {

            //收藏后return 返回true代表收藏,false代表取消收藏,就不用查询出来
            boolean ref = travelStatisRedisService.FavorByTravelId(vo.getTravelId(), userInfo);



        }
        return "/mine/travelcollection";
    }*/
    //我的收藏(游记)
    @RequestMapping("/travelcollection")
    @RequireLogin
    public String travelCollection(Model model, @UserParam UserInfo userInfo) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user",user);

        //获取到所有的用户收藏的游记id
        List<Long> travelList2 =  travelStatisRedisService.selectCollectionTravelList2(userInfo.getId());
        List<Travel> list = new ArrayList<>();

        for (Long aLong : travelList2) {

            Travel travel = travelService.selectTravelById(aLong);
            list.add(travel);
        }
        model.addAttribute("pageInfo",list);

        return "/mine/travelcollection";
    }


    //我的收藏(攻略)
    @RequestMapping("/strategycollection")
    @RequireLogin
    public String strategyCollection(Model model, @UserParam UserInfo userInfo) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user",user);

        //获取到所有的用户收藏的游记id
        List<Long> strategyList2 =  strategyStatisRedisService.selectCollectionTravelList2(userInfo.getId());
        List<StrategyDetail> list = new ArrayList<>();

        for (Long aLong : strategyList2) {
            //selectStrategyDetailById
            StrategyDetail strategyDetail = strategyDetailService.get(aLong);
            list.add(strategyDetail);
        }
        model.addAttribute("pageInfo",list);

        return "/mine/strategycollection";
    }



    //我的点评
    @RequestMapping("/review")
    @RequireLogin
    public String review(Model model, @UserParam UserInfo userInfo, @ModelAttribute("qo")AttractionsQuery qo)  {
        UserInfo user = userInfoService.selectByPrimaryKey(userInfo.getId());
        model.addAttribute("user",user);

        List<Attractions> reviewSize= attractionsService.selectReviewSize(userInfo.getId());//用户点评的总数
        int sizes = 0;
        for (Attractions attractions : reviewSize) {
           if (attractions.getReviewNum() > 0 && attractions.getReviewNum() != null){
               sizes ++;
           }
        }


        model.addAttribute("sizes", sizes);
        model.addAttribute("reviews", attractionsService.selectForList(qo ,userInfo.getId()));


        return "/mine/review";
    }


    //点击用户头像显示404
    @RequestMapping("/miss")
    @RequireLogin
    public String miss() {

        return "/mine/miss";
    }











}
